//
//  FMCategoryViewController.m
//  PsychologyFM
//
//  Created by lanou on 16/1/14.
//  Copyright © 2016年 ZhangLuDu. All rights reserved.
//

#import "FMCategoryViewController.h"
#import "FMSelectTableViewCell.h"
#import "FMCategoryModel.h"
#import "FMLessonAndFMModel.h"
#import "FMDiantaiPlayViewController.h"
#import "MJRefresh.h"

@interface FMCategoryViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) NSMutableArray *dataArray;

@property (nonatomic,strong) UIButton *backButton;
@property (nonatomic,strong) UIButton *radioButton;

@property (nonatomic,assign) NSInteger offset;

@end

@implementation FMCategoryViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.offset = 0;
    
    
    //  添加navigationitem
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back"] style:(UIBarButtonItemStylePlain) target:self action:@selector(backBarButtonItemClick:)];
    self.navigationItem.leftBarButtonItem = backItem;
    
    
    

    self.view.backgroundColor = FMWhite;
    
    //  添加tableview视图
    _categoryTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight) style:(UITableViewStylePlain)];
    _categoryTableView.dataSource = self;
    _categoryTableView.delegate = self;
    _categoryTableView.rowHeight = 70;
    
    //  注册cell
    [_categoryTableView registerClass:[FMSelectTableViewCell class] forCellReuseIdentifier:@"FMSelectTableViewCell"];
    [self.view addSubview:_categoryTableView];
    
    
    //  请求数据
    [self requestData];
    
#warning --
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
//    self.categoryTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        // 进入刷新状态后会自动调用这个block
//        _offset += 20;
//        [self requestData];
//    }];
//
    self.categoryTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        _offset += 20;
        [self requestData];
    }];
    
    // 马上进入刷新状态
    [self.categoryTableView.mj_header beginRefreshing];
    [self.categoryTableView.mj_footer beginRefreshing];
    
}

- (void)requestData
{

    if (_category_id) {
        _urlStr = [NSString stringWithFormat:@"http://yiapi.xinli001.com/fm/category-jiemu-list.json?key=c0d28ec0954084b4426223366293d190&category_id=%@&limit=0&offset=%@",_category_id,[NSString stringWithFormat:@"%ld",(long)self.offset]];
    }else
    {
        //  根据navigationItem.title 不同确定 url的拼接 
        if ([self.navigationItem.title isEqualToString:@"最新心理课"]) {
            _urlStr = [NSString stringWithFormat:@"http://bapi.xinli001.com/fm2/broadcast_list.json/?is_teacher=1&key=c0d28ec0954084b4426223366293d190&rows=15&offset=%@",[NSString stringWithFormat:@"%ld",(long)(long)self.offset]];
        }else if([self.navigationItem.title isEqualToString:@"最新电台"])
         _urlStr = [NSString stringWithFormat:@"http://bapi.xinli001.com/fm2/broadcast_list.json/?is_teacher=0&key=c0d28ec0954084b4426223366293d190&rows=15&offset=%@",[NSString stringWithFormat:@"%ld",(long)(long)self.offset]];
    }
    if (_offset == 0) {
        [_dataArray removeAllObjects];
    }
    if (self.dataArray == nil) {
        _dataArray = [NSMutableArray array];

    }

    [PKRequestManager requestWithUrlString:_urlStr parDic:nil method:GET finish:^(NSData *data) {
//        NSLog(@"%@",_urlStr);
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//        NSLog(@"%@",dic);
            NSArray *array = dic[@"data"];
            for (NSDictionary *smDic in array) {
                FMLessonAndFMModel *model = [[FMLessonAndFMModel alloc]init];
                [model setValuesForKeysWithDictionary:smDic];
                [_dataArray addObject:model];
            }
        
            [_categoryTableView.mj_header endRefreshing];
            [_categoryTableView.mj_footer endRefreshing];
            //  一定要刷新界面
            [_categoryTableView reloadData];
            
        } error:^(NSError *error) {
            NSLog(@"%@",error);
        }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FMSelectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FMSelectTableViewCell"];
    FMLessonAndFMModel *model = _dataArray[indexPath.row];
    cell.homeModel = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FMLessonAndFMModel *model = _dataArray[indexPath.row];
    FMDiantaiPlayViewController *diantaiVC = [[FMDiantaiPlayViewController alloc]init];
    diantaiVC.value = model.id;
    diantaiVC.rootVC = _rootVC;
    [self.navigationController pushViewController:diantaiVC animated:YES];
}


- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
    self.automaticallyAdjustsScrollViewInsets  = NO;
    
    _rootVC.scrollView.height += 49;
//    _rootVC.tabbar.hidden = YES;
    [_rootVC.tabbar removeFromSuperview];
    
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    _rootVC.scrollView.height -= 49;
    [_rootVC.view addSubview:_rootVC.tabbar];


    [self showTabbar];
}

- (void)showTabbar
{
    self.rootVC.tabbar.hidden = NO;
}

- (void)backBarButtonItemClick:(UIBarButtonItem *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
